/**
 * @FileName: HotspotKnowledgeProxy.java
 * @Package: com.ziroom.zmc.services.knowledge.proxy
 * @author youshipeng
 * @created 2016/10/12 10:12
 * <p>
 * Copyright 2016 ziroom
 */
package top.swimmer.provider.proxy;

import com.alibaba.druid.support.json.JSONUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Component;
import top.swimmer.provider.api.inner.PinyinWordInnerService;
import top.swimmer.provider.api.outer.PinyinWordOuterService;
import top.swimmer.provider.dao.elasticsearch.KnowledgeInformationMapper;
import top.swimmer.provider.model.elasticsearch.KnowledgeInformation;
import top.swimmer.provider.model.value.WordClickCount;
import top.swimmer.provider.service.PinyinWordService;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.*;
import static top.swimmer.tools.collection.CollectionUtils.isEmpty;

/**
 * <p></p>
 *
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author youshipeng
 * @since 1.0
 * @version 1.0
 */
//@Component("pinyinWordProxy")
public class PinyinWordProxy implements PinyinWordInnerService, PinyinWordOuterService {

    @Resource(name = "knowledgeInformationMapper")
    KnowledgeInformationMapper knowledgeInformationMapper;

    @Resource(name = "pinyinWordService")
    private PinyinWordService pinyinWordService;

    @Override
    public String analyzeSearch(String input) throws Exception {
        List<List<WordClickCount>> results = pinyinWordService.analyzeSearch(input);
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        Set<String> content = new HashSet<>();
        Set<String> must = new HashSet<>();
        StringBuilder sb = new StringBuilder("");
        Double matchCount = 0.0;
        for (List<WordClickCount> son : results) {
            if (!isEmpty(son) && son.size() == 1) {
                boolQueryBuilder.must(termQuery("pushContent", son.get(0).getWord()));
                continue;
            }
            matchCount += 1.0;
            for (WordClickCount clickCount : son) {
                sb.append(clickCount.getWord());
                content.add(clickCount.getWord());
            }
        }

        Double total = Double.valueOf(content.size());

        int minimumMatch = matchCount.equals(total) ? 99 : (int) (matchCount / total * 100) + 1;
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        boolQueryBuilder.must(termQuery("cityCode", "110000"));

        searchSourceBuilder.query(boolQueryBuilder);
        if (total != 0) {
            searchSourceBuilder.postFilter(multiMatchQuery(sb.toString(), "pushContent").minimumShouldMatch(minimumMatch + "%"));
        }
        searchSourceBuilder.highlight(
                new HighlightBuilder().field("pushContent").preTags("@preTag@").postTags("@postTag@")
        );
        searchSourceBuilder.from(0).size(15).sort("_score", SortOrder.DESC).sort("clicks", SortOrder.DESC);

        final List<KnowledgeInformation> knowledgeInformation = knowledgeInformationMapper.search(searchSourceBuilder);
        return "dddddd";
    }
}